package com.zhongying.jf.util.sms253;

import com.alibaba.fastjson.JSON;
import com.zhongying.jf.util.sms253.model.SmsReportReponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 解析回送状态报告
 */
public class SmsReportReceiverUtil {

    private static final Logger logger = LoggerFactory.getLogger(SmsReportReceiverUtil.class);

    public static void main(String[] args) {
        String url = "http://client_url?receiver=admin&pswd=12345&msgid=17041010383624511&reportTime=1704101038&mobile=15700000004&notifyTime=170410103838&uid=17041010383624511&status=DELIVRD&statusDesc=短信发送成功";
        SmsReportReponse res = getReportReceiverReponse(url);
        System.out.println("res=" + JSON.toJSONString(res));
    }

    /**
     * 返回回送状态报告响应结果
     *
     * @param url 用户账户设置的状态报告回送地址
     * @return
     */
    public static SmsReportReponse getReportReceiverReponse(String url) {
        SmsReportReponse res = new SmsReportReponse();
        Map<String, String> mapRequest = URLRequest(url);
        res.setReceiver(mapRequest.get("receiver"));
        res.setPswd(mapRequest.get("pswd"));
        res.setMsgid(mapRequest.get("msgid"));
        res.setReportTime(mapRequest.get("reportTime"));
        res.setMobile(mapRequest.get("mobile"));
        res.setNotifyTime(mapRequest.get("notifyTime"));
        res.setUid(mapRequest.get("uid"));
        res.setStatus(mapRequest.get("status"));
        res.setStatusDesc(CodeUtil.decode(mapRequest.get("statusdesc")));
        return res;
    }


    /**
     * 去掉url中的路径，留下请求参数部分
     *
     * @param strURL url地址
     * @return url请求参数部分
     */
    private static String TruncateUrlPage(String strURL) {
        String strAllParam = null;
        String[] arrSplit = null;
        strURL = strURL.trim().toLowerCase();
        arrSplit = strURL.split("[?]");
        if (strURL.length() > 1) {
            if (arrSplit.length > 1) {
                if (arrSplit[1] != null) {
                    strAllParam = arrSplit[1];
                }
            }
        }
        return strAllParam;
    }

    /**
     * 解析出url参数中的键值对
     * 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
     *
     * @param URL url地址
     * @return url请求参数部分
     */
    private static Map<String, String> URLRequest(String URL) {
        Map<String, String> mapRequest = new HashMap<String, String>();
        try {
            String[] arrSplit = null;
            String strUrlParam = TruncateUrlPage(URL);
            if (strUrlParam == null) {
                return mapRequest;
            }
            //每个键值为一组 www.2cto.com
            arrSplit = strUrlParam.split("[&]");
            for (String strSplit : arrSplit) {
                String[] arrSplitEqual = null;
                arrSplitEqual = strSplit.split("[=]");
                //解析出键值
                if (arrSplitEqual.length > 1) {
                    //正确解析
                    mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
                } else {
                    if (arrSplitEqual[0] != "") {
                        //只有参数没有值，不加入
                        mapRequest.put(arrSplitEqual[0], "");
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info("参数解析失败:" + ex.getMessage());
        }
        return mapRequest;
    }


}
